// 
// Copyright (c) 2006-2010 Ben Motmans
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Author(s):
//    Ben Motmans <ben.motmans@gmail.com>
//

using System;
using System.Text;

namespace Anculus.Core
{
	public class DefaultLogFormatter : ILogFormatter
	{
		public const string DefaultTimeStampFormat = "yyyy/MM/dd HH:mm:ss.fffffff K";
		
		protected string timeStampFormat;

		public bool ShowTimeStamp { get; set; }
		public bool ShowThreadInfo { get; set; }
		public bool ShowLogger { get; set; }
		public bool ShowSource { get; set; }

		public string TimeStampFormat
		{
			get { return timeStampFormat; }
			set {
				if (String.IsNullOrEmpty (value))
					timeStampFormat = DefaultTimeStampFormat;
				timeStampFormat = value;
			}
		}
		
		public string Format (LogEntry entry)
		{
			if (entry == null)
				return null;
			
			return String.Concat (
				GetLogLevelString (entry.LogLevel), " ",
				ShowTimeStamp ? entry.TimeStamp.ToString (timeStampFormat) + " " : "",
				ShowThreadInfo ? String.Concat ("[", entry.ThreadName, ":", entry.ThreadId, "] ") : "",
				ShowLogger ? entry.Logger : "", ": ",
				entry.Message,
				ShowSource ? entry.Source : "",
				Environment.NewLine,
				FormatException (entry.Exception)
			);
		}
		
		public void LoadSettings (IConfigurationSection section)
		{
			timeStampFormat = section.GetString ("TimeStampFormat", DefaultTimeStampFormat);
			
			ShowTimeStamp = section.GetBool ("ShowTimeStamp", true);
			ShowThreadInfo = section.GetBool ("ShowThreadInfo", true);
			ShowLogger = section.GetBool ("ShowLogger", true);
			ShowSource = section.GetBool ("ShowSource", true);
		}

		public static string GetLogLevelString (LogLevel level)
		{
			switch (level)
			{
				case LogLevel.Trace:
					return "TRACE";
				case LogLevel.Debug:
					return "DEBUG";
				case LogLevel.Info:
					return "INFO";
				case LogLevel.Warning:
					return "WARNING";
				case LogLevel.Error:
					return "ERROR";
				case LogLevel.Fatal:
					return "FATAL";
				default:
					return String.Empty;
			}
		}
		
		protected string FormatException (Exception ex)
		{
			if (ex == null)
				return String.Empty;
			
			string exinfo = String.Concat (Environment.NewLine, "\texception: ",
				ex.Message, Environment.NewLine,
				ex.StackTrace,
				FormatException (ex.InnerException),
				Environment.NewLine
			);

			return exinfo;
		}

		
	}
}
